<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="search-container">
        <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-right:1rem;" @click="handleFilter">
          查询
        </el-button>
        <!-- <el-badge value="显示已核查" type="info" class="item" style="margin-left:1rem;">
          <el-switch
            v-model="switchBtn"
            active-color="#13ce66"
            style="margin-top:0.5rem;"
            @change="handleChangeSwitch"
          />
        </el-badge> -->
        <el-radio-group v-model="radioBtn" @change="handleChangeRadio">
          <el-radio-button :label="0">未核查</el-radio-button>
          <el-radio-button :label="1">已核查</el-radio-button>
        </el-radio-group>
        <el-tooltip placement="right">
          <span style="line-height:2.4rem;margin-left:0.5rem;">评分标准：<i class="el-icon-warning-outline" /></span>
          <div slot="content" class="tips">
            <span>1.1分（结果非常好，超出预期）</span>
            <br>
            <br>
            <span>1分（按质按量完成）</span>
            <br>
            <br>
            <span>0.8分（质量有所欠缺）</span>
            <br>
            <br>
            <span>0.6分（工作逾期）</span>
            <br>
            <br>
            <span>0.1~0.5分（严重逾期或质量较差）</span>
            <br>
            <br>
            <span>0分（截至时间为本月内，但到月底最后一天仍未完成的，系统自动判0分）</span>
            <br>
            <br>
            <span>如月底24时还有未被核查的任务，该任务系统自动判0.9分</span>
          </div>
        </el-tooltip>
      </div>
      <pagination v-show="total>0" :total="total" :page.sync="searchQuery.pageNo" :layout="'total, sizes, prev, pager, next, jumper'" :limit.sync="searchQuery.pageSize" @pagination="getManageDatas" />
    </div>

    <el-table
      :key="tableKey"
      v-loading="pageLoading"
      :data="listData"
      border
      fit
      :row-class-name="tableRowClassName"
      style="width: 100%;"
    >
      <el-table-column label="名称" align="left">
        <template slot-scope="{row}">
          <span v-if="new Date(row.deadline_at).getTime() - new Date().getTime() < 0 && row.status !== 3" style="color:#F56C6C;"><i class="el-icon-time" />「{{ row.deadline_at | filterFormatTime2 }}」</span>
          <span v-if="(new Date(row.deadline_at).getTime() - new Date(row.complete_at).getTime() < 0) && row.status === 3" style="color:#F56C6C;"><i class="el-icon-time" />「逾期完成」</span>
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务系数" width="50px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.weight }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="80px" align="center">
        <template slot-scope="{row}">
          <el-tag effect="light" :type="row.status | golbalStatusFilter">
            {{ row.status | golbalStatusNameFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="优先级" width="80px" align="center">
        <template slot-scope="{row}">
          <el-tag effect="dark" :type="row.priority | golbalPriorityFilter">
            {{ row.priority | golbalPriorityNameFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="归属项目" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.project.name || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行人" width="150px" align="center">
        <template slot-scope="{row}">
          <span v-if="!row.execute_by.length">-</span>
          <span v-for="(user, idx) of row.execute_by" :key="user.id">{{ user.nickname }}{{ idx === row.execute_by.length - 1 ? '' : '、' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核查状态" width="50px" align="center">
        <template slot-scope="{row}">
          <i v-if="row.review_status" class="el-icon-check" style="color: #67C23A" />
          <i v-else class="el-icon-close" />
        </template>
      </el-table-column>
      <el-table-column label="评分" width="180px" align="center">
        <template slot-scope="{row}">
          <el-input-number v-model="row.score" size="small" :precision="1" :step="0.1" :min="0" :max="1.1" style="width:100px;margin-right:0.5rem;" controls-position="right" @change="handleChangeScore" />
          <el-button type="success" size="small" icon="el-icon-check" circle @click.stop="handleCheckTaskScore(row)" />
        </template>
      </el-table-column>
      <el-table-column v-if="radioBtn" label="评分" width="50px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.score }}</span>
        </template>
      </el-table-column>
      <el-table-column label="截止时间" width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.deadline_at | golbalParseTimeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.complete_at | golbalParseTimeFilter }}</span>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import * as ManageApi from '@/api/manage'

const dateMonth = new Date()
dateMonth.setMonth(dateMonth.getMonth())
dateMonth.setDate(1)

export default {
  name: 'Check',
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      total: 0,
      select_month: dateMonth,
      radioBtn: 0,
      tempScore: 0,
      searchQuery: {
        pageNo: 1,
        pageSize: 10,
        review_status: 0,
        status: 3
      },
      pageLoading: false,
      listData: []
    }
  },
  mounted() {
    this.getManageDatas()
  },
  methods: {
    async getManageDatas(pageQuery) {
      if (pageQuery) {
        this.searchQuery = Object.assign(this.searchQuery, pageQuery)
      }
      this.listData = []
      this.pageLoading = true
      this.searchQuery['review_status'] = this.radioBtn ? 1 : null
      const { data } = await ManageApi.getManageTaskChecks(this.searchQuery)
      if (data && data.total) {
        this.listData = data.items
        this.total = data.total
      }
      this.pageLoading = false
    },
    tableRowClassName({ row }) {
      if (row.status === 3 || row.progress === 100) {
        if ((new Date(row.deadline_at).getTime() - new Date(row.complete_at).getTime() < 0)) {
          return 'not-done-row'
        }
        return 'done-row'
      } else if ((new Date(row.deadline_at).getTime() - new Date().getTime() < 0)) {
        return 'not-done-row'
      }
    },
    handleFilter() {
      this.searchQuery.pageNo = 1
      this.getManageDatas()
    },
    handleChangeRadio(val) {
      this.getManageDatas()
    },
    handleChangeScore(e) {
      this.tempScore = e
    },
    async handleCheckTaskScore(row) {
      const { data } = await ManageApi.updateTaskCheck({
        id: row.id,
        score: this.tempScore
      })
      if (data) {
        await this.$notify({
          title: 'Success',
          message: '评分成功',
          type: 'success',
          duration: 2000
        })
        this.getManageDatas()
      }
    }
  }
}
</script>

<style scoped>
.el-table /deep/ .done-row {
  background: #f0f9eb;
}
.el-table /deep/ .not-done-row {
  background: #fde9e9;
}
.filter-container {
  display: flex;
  justify-content: space-between;
}
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 1rem;
}
.search-container {
  display: flex;
  align-items: flex-start;
}
</style>
